/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ControlPrenez.java
 *
 * Created on 14-dic-2011, 13:05:34
 */
package formularios;

import clases.ConversorRSaDefaultTableModel;
import clases.ProcesarTxt;
import clases.abmBovino;
import clases.conexion;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Elio
 */
public class ControlPeso extends javax.swing.JDialog {
    String consultainicial="";
  //  DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    /** Creates new form ControlPrenez */
    public ControlPeso(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setTitle("Control de Peso");
        consultainicial="SELECT c.identificacion, c.fecha, tb.descripcion, c.detalle FROM controles c,"
                + " bovino b ,tipobovino tb where idControl=4 and b.identificacion=c.identificacion and b.idtipobovino=tb.idtipobovino order by c.fecha desc";
        CrearTabla(consultainicial);
    //    calendarioDesde.setDateFormat(df);
   //     calendarioDesde.repaint();
        
        
       //calendarioDesde.setDateFormat(java.text.DateFormat.);
    }
    
    public void Conectar(){
        try{
            mdbc = new conexion();
            mdbc.init();
            Connection conn= mdbc.getMyConnection();
            stmt = conn.createStatement();
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Problemas al concetarse a la Base de Datos","Error",JOptionPane.ERROR_MESSAGE);
        }
    }
    
     private void CrearTabla(String sql){
      
          try {
            Conectar();
            ResultSet rs = stmt.executeQuery(sql);

      
            tablaPeso.setModel(modelo);
            tablaPeso.setAutoResizeMode(tablaPeso.AUTO_RESIZE_ALL_COLUMNS);
              
                    
            ConversorRSaDefaultTableModel.completar(rs, modelo);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error en la consulta de control preñez" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        
    }
    
    private conexion mdbc;
    private Statement stmt;
    private DefaultTableModel modelo = new DefaultTableModel();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnNuevoControlPreñez = new javax.swing.JButton();
        panelConsultas = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaPeso = new javax.swing.JTable();
        tabPanelBuscarAnimal = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtBuscarAnimalPreñez = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cbTipoBovino = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        calendarioDesdePreñez = new org.freixas.jcalendar.JCalendarCombo();
        jLabel4 = new javax.swing.JLabel();
        calendarioHastaPreñez = new org.freixas.jcalendar.JCalendarCombo();
        jLabel5 = new javax.swing.JLabel();
        btnBuscarFechasPreñez = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Control Peso");

        jPanel1.setLayout(new java.awt.GridLayout(1, 2));

        btnNuevoControlPreñez.setText("Nuevo Control");
        btnNuevoControlPreñez.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoControlPreñezActionPerformed(evt);
            }
        });
        jPanel1.add(btnNuevoControlPreñez);

        tablaPeso.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tablaPeso);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Buscar:");

        txtBuscarAnimalPreñez.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscarAnimalPreñezKeyReleased(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel3.setText("(Ingrese el Nº de identificación del animal)");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Seleccione el tipo de bovino:");

        cbTipoBovino.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Ternero", "Vaquillona", "Vaca", "Torito", "Toro ", "Novillo" }));
        cbTipoBovino.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTipoBovinoItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtBuscarAnimalPreñez, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(cbTipoBovino, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(191, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBuscarAnimalPreñez, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(27, 27, 27)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cbTipoBovino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        tabPanelBuscarAnimal.addTab("Buscar por Animal", jPanel3);

        calendarioDesdePreñez.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "10-03-2012" }));
        calendarioDesdePreñez.setDateFormat(new SimpleDateFormat("dd/MM/yyyy"));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Desde:");

        calendarioHastaPreñez.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "10-03-2012" }));
        calendarioHastaPreñez.setDateFormat(new SimpleDateFormat("dd/MM/yyyy"));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Hasta:");

        btnBuscarFechasPreñez.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/buscar.png"))); // NOI18N
        btnBuscarFechasPreñez.setText("Buscar");
        btnBuscarFechasPreñez.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarFechasPreñezActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(52, 52, 52)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(calendarioHastaPreñez, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(calendarioDesdePreñez, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(80, 80, 80)
                .addComponent(btnBuscarFechasPreñez)
                .addGap(187, 187, 187))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(calendarioDesdePreñez, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(calendarioHastaPreñez, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(63, 63, 63))
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnBuscarFechasPreñez)
                .addContainerGap(76, Short.MAX_VALUE))
        );

        tabPanelBuscarAnimal.addTab("Buscar por Fechas", jPanel4);

        javax.swing.GroupLayout panelConsultasLayout = new javax.swing.GroupLayout(panelConsultas);
        panelConsultas.setLayout(panelConsultasLayout);
        panelConsultasLayout.setHorizontalGroup(
            panelConsultasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelConsultasLayout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(panelConsultasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabPanelBuscarAnimal, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 546, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40))
        );
        panelConsultasLayout.setVerticalGroup(
            panelConsultasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelConsultasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabPanelBuscarAnimal, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
                            .addComponent(panelConsultas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 544, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(panelConsultas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-649)/2, (screenSize.height-489)/2, 649, 489);
    }// </editor-fold>//GEN-END:initComponents

private void btnNuevoControlPreñezActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoControlPreñezActionPerformed
    new ProcesarTxt().LeerControlPeso(4);
    CrearTabla(consultainicial);
    txtBuscarAnimalPreñez.setText("");
}//GEN-LAST:event_btnNuevoControlPreñezActionPerformed

    private void btnBuscarFechasPreñezActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarFechasPreñezActionPerformed
    
        String fechadesde = convertirFecha(calendarioDesdePreñez.getSelectedItem().toString());  
        String fechahasta = convertirFecha(calendarioHastaPreñez.getSelectedItem().toString());  
         String consulta= "SELECT c.identificacion, c.fecha, tb.descripcion, c.detalle FROM controles c, bovino b ,tipobovino tb where idControl=4 and b.identificacion=c.identificacion and "
                + "b.idtipobovino=tb.idtipobovino and c.fecha between '"+fechadesde+"' and '"+fechahasta+"'";
        
        CrearTabla(consulta);
    }//GEN-LAST:event_btnBuscarFechasPreñezActionPerformed

    private void txtBuscarAnimalPreñezKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarAnimalPreñezKeyReleased
 String consulta = "SELECT c.identificacion, c.fecha, tb.descripcion,"
         + " "+ "c.detalle FROM controles c, bovino b ,tipobovino tb "
         + "where"+ " idControl=4 and b.identificacion=c.identificacion and "
         + "c.identificacion LIKE '%" + txtBuscarAnimalPreñez.getText() + "%' and b.idtipobovino=tb.idtipobovino "
         + "order by c.fecha desc";          
 
 CrearTabla(consulta);     

    }//GEN-LAST:event_txtBuscarAnimalPreñezKeyReleased

    private void cbTipoBovinoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbTipoBovinoItemStateChanged
                String consulta = "";
                
                if (cbTipoBovino.getSelectedItem().equals("Todos")) {
                    consulta = "SELECT c.identificacion, c.fecha, tb.descripcion,c.detalle FROM controles c,"
                            + " bovino b ,tipobovino tb where idControl=4 and b.identificacion=c.identificacion and"
                            + " c.identificacion LIKE '%" + txtBuscarAnimalPreñez.getText() + "%' and b.idtipobovino=tb.idtipobovino " 
                            + "order by c.fecha desc";         
                
                }
                
                else {             consulta = "SELECT c.identificacion, c.fecha, tb.descripcion, c.detalle FROM controles c,"
                        + " bovino b ,tipobovino tb where idControl=4 and"
                        + " b.identificacion=c.identificacion and"
                        + " c.identificacion LIKE '%" + txtBuscarAnimalPreñez.getText() + "%' and b.idtipobovino=tb.idtipobovino "
                        + "and c.detalle='"+cbTipoBovino.getSelectedItem() + "' order by c.fecha desc"; 
                }
                CrearTabla(consulta);                                                    
    }//GEN-LAST:event_cbTipoBovinoItemStateChanged

     public String convertirFecha(String FechaA){
      String anio,mes,dia;
     //30112011
      anio=FechaA.substring(6,10);
      mes=FechaA.substring(3,5);
      dia=FechaA.substring(0,2);
      String Fecha=anio+"-"+mes+"-"+dia;
      return Fecha;

    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ControlPrenez.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ControlPrenez.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ControlPrenez.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ControlPrenez.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ControlPeso dialog = new ControlPeso(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
        
        //jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL))));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarFechasPreñez;
    private javax.swing.JButton btnNuevoControlPreñez;
    private org.freixas.jcalendar.JCalendarCombo calendarioDesdePreñez;
    private org.freixas.jcalendar.JCalendarCombo calendarioHastaPreñez;
    private javax.swing.JComboBox cbTipoBovino;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelConsultas;
    private javax.swing.JTabbedPane tabPanelBuscarAnimal;
    private javax.swing.JTable tablaPeso;
    private javax.swing.JTextField txtBuscarAnimalPreñez;
    // End of variables declaration//GEN-END:variables
}
